{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "特征工程是什么？\n",
    "特征工程是将原始数据转换为更好地代表预测模型的潜在问题的特征的过程，从而提高了对未知数据的模型准确性\n",
    "\n",
    "注：特征值化是为了计算机更好的去理解数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler, Imputer\n",
    "from sklearn.feature_selection import VarianceThreshold\n",
    "from sklearn.decomposition import PCA\n",
    "import jieba\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特征抽取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['dislike', 'is', 'life', 'like', 'long', 'python', 'short', 'too']\n",
      "[[0 1 1 1 0 1 1 0]\n",
      " [1 1 1 0 1 1 0 1]]\n"
     ]
    }
   ],
   "source": [
    "# 导入包\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "\n",
    "# 实例化CountVectorizer\n",
    "vector = CountVectorizer()\n",
    "\n",
    "# 调用fit_transform输入并转换数据\n",
    "res = vector.fit_transform([\"life is short,i like python\",\"life is too long,i dislike python\"])\n",
    "\n",
    "# 打印结果\n",
    "print(vector.get_feature_names())\n",
    "print(res.toarray())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['city=上海', 'city=北京', 'city=深圳', 'temperature']\n",
      "[{'city=北京': 1.0, 'temperature': 100.0}, {'city=上海': 1.0, 'temperature': 60.0}, {'city=深圳': 1.0, 'temperature': 30.0}]\n",
      "[[  0.   1.   0. 100.]\n",
      " [  1.   0.   0.  60.]\n",
      " [  0.   0.   1.  30.]]\n"
     ]
    }
   ],
   "source": [
    "# 字典数据抽取\n",
    "# 实例化\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 调用fit_transform\n",
    "data = dict.fit_transform([{'city': '北京','temperature': 100}, {'city': '上海','temperature':60}, {'city': '深圳','temperature': 30}])\n",
    "\n",
    "print(dict.get_feature_names())\n",
    "\n",
    "print(dict.inverse_transform(data))\n",
    "\n",
    "print(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.28620952e-15  3.82970843e+00]\n",
      " [ 5.74456265e+00 -1.91485422e+00]\n",
      " [-5.74456265e+00 -1.91485422e+00]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "def dictvec():\n",
    "    \n",
    "    return None\n",
    "\n",
    "\n",
    "def countvec():\n",
    "    \"\"\"\n",
    "    对文本进行特征值化\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    cv = CountVectorizer()\n",
    "\n",
    "    data = cv.fit_transform([\"人生 苦短，我 喜欢 python\", \"人生漫长，不用 python\"])\n",
    "\n",
    "    print(cv.get_feature_names())\n",
    "\n",
    "    print(data.toarray())\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def cutword():\n",
    "\n",
    "    con1 = jieba.cut(\"今天很残酷，明天更残酷，后天很美好，但绝对大部分是死在明天晚上，所以每个人不要放弃今天。\")\n",
    "\n",
    "    con2 = jieba.cut(\"我们看到的从很远星系来的光是在几百万年之前发出的，这样当我们看到宇宙时，我们是在看它的过去。\")\n",
    "\n",
    "    con3 = jieba.cut(\"如果只用一种方式了解某样事物，你就不会真正了解它。了解事物真正含义的秘密取决于如何将其与我们所了解的事物相联系。\")\n",
    "\n",
    "    # 转换成列表\n",
    "    content1 = list(con1)\n",
    "    content2 = list(con2)\n",
    "    content3 = list(con3)\n",
    "\n",
    "    # 吧列表转换成字符串\n",
    "    c1 = ' '.join(content1)\n",
    "    c2 = ' '.join(content2)\n",
    "    c3 = ' '.join(content3)\n",
    "\n",
    "    return c1, c2, c3\n",
    "\n",
    "\n",
    "\n",
    "def hanzivec():\n",
    "    \"\"\"\n",
    "    中文特征值化\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    c1, c2, c3 = cutword()\n",
    "\n",
    "    print(c1, c2, c3)\n",
    "\n",
    "    cv = CountVectorizer()\n",
    "\n",
    "    data = cv.fit_transform([c1, c2, c3])\n",
    "\n",
    "    print(cv.get_feature_names())\n",
    "\n",
    "    print(data.toarray())\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "\n",
    "def tfidfvec():\n",
    "    \"\"\"\n",
    "    中文特征值化\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    c1, c2, c3 = cutword()\n",
    "\n",
    "    print(c1, c2, c3)\n",
    "\n",
    "    tf = TfidfVectorizer()\n",
    "\n",
    "    data = tf.fit_transform([c1, c2, c3])\n",
    "\n",
    "    print(tf.get_feature_names())\n",
    "\n",
    "    print(data.toarray())\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "def mm():\n",
    "    \"\"\"\n",
    "    归一化处理\n",
    "    :return: NOne\n",
    "    \"\"\"\n",
    "    mm = MinMaxScaler(feature_range=(2, 3))\n",
    "\n",
    "    data = mm.fit_transform([[90,2,10,40],[60,4,15,45],[75,3,13,46]])\n",
    "\n",
    "    print(data)\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "def stand():\n",
    "    \"\"\"\n",
    "    标准化缩放\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    std = StandardScaler()\n",
    "\n",
    "    data = std.fit_transform([[ 1., -1., 3.],[ 2., 4., 2.],[ 4., 6., -1.]])\n",
    "\n",
    "    print(data)\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "def im():\n",
    "    \"\"\"\n",
    "    缺失值处理\n",
    "    :return:NOne\n",
    "    \"\"\"\n",
    "    # NaN, nan\n",
    "    im = Imputer(missing_values='NaN', strategy='mean', axis=0)\n",
    "\n",
    "    data = im.fit_transform([[1, 2], [np.nan, 3], [7, 6]])\n",
    "\n",
    "    print(data)\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "def var():\n",
    "    \"\"\"\n",
    "    特征选择-删除低方差的特征\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    var = VarianceThreshold(threshold=1.0)\n",
    "\n",
    "    data = var.fit_transform([[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]])\n",
    "\n",
    "    print(data)\n",
    "    return None\n",
    "\n",
    "\n",
    "def pca():\n",
    "    \"\"\"\n",
    "    主成分分析进行特征降维\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    pca = PCA(n_components=0.9)\n",
    "\n",
    "    data = pca.fit_transform([[2,8,4,5],[6,3,0,8],[5,4,9,1]])\n",
    "\n",
    "    print(data)\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    pca()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
